<?php
namespace app\api\controller;

use app\api\controller\Common;
use app\api\validate\UniversalValidate;
use think\Controller;
use ztouch\ZeroTouch;
use think\Db;
use think\helper\Time;

class Login extends Controller
{
    protected $common;

    // 初始化方法
    public function initialize()
    {
        parent::initialize();
        $this->common = new Common();
    }

    // 获得用户的tx-sig
    public function getUserSig()
    {
        $userid = request()->param('user_id');
        $info   = $this->common->getUserSig($userid);
        halt($info);
    }

    /**
     * 未登录
     * @return [type] [description]
     */
    public function notLogin()
    {
        return json_return(398, '未获取登陆信息或登陆信息过期，请重新登陆');
    }

    /**
     * 登陆过期
     */
    public function expireLogin()
    {
        return json_return(397, 'token过期，请重新登陆');
    }

    /**
     * 验证登陆
     */
    public function checkLogin()
    {
        if (Request()->isPost()) {
            $data     = Request()->param();
            $validate = new UniversalValidate();
            if (!$validate->scene('checkLogin')->check($data)) {
                return json_return(301, $validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            // 验证验证码
            $ztouch = new ZeroTouch();
            // $sys_code = $ztouch->checkVerify($data['phone'], $data['code']); // 验证验证码
            $sys_code = true;
            if ($sys_code == true) {
                $user_info = $this->common->check_phone($data['phone'], Request()->ip(), $data['remote_device'], $data['lat'], $data['lng']);
                return $user_info ? json_return(200, '用户登陆成功', ['user_info' => $user_info]) : json_return(303, '网络繁忙，请稍后再试');
            } else {
                return json_return(302, '验证码不正确');
            }
        } else {
            return json_return(399, '非法请求');
        }
    }

    /**
     * 微信登陆
     * @return [type] [description]
     */
    public function checkWx()
    {
        if (request()->isPost()) {
            $data     = Request()->param();
            $validate = new UniversalValidate();
            if (!$validate->scene('checkWx')->check($data)) {
                return json_return(301, $validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            $data['ip'] = request()->ip();
            $res        = $this->common->checkWx($data);
            switch ($res['type']) {
                case 1:
                    return json_return(201, '记录微信用户成功', $res); // 未绑定手机号
                    break;
                case 2:
                    return json_return(200, '微信用户登陆成功', $res); // 已绑定手机号
                    break;
                default:
                    return json_return(302, '系统繁忙，请联系管理员');
                    break;
            }
        } else {
            return json_return(399, '非法请求');
        }

    }

    // 微信绑定手机号
    public function bindingPhoneWithWx()
    {
        if (request()->isPost()) {
            $data     = Request()->param();
            $validate = new UniversalValidate();
            if (!$validate->scene('bindingWx')->check($data)) {
                return json_return(301, $validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            // 验证验证码
            // $ztouch = new ZeroTouch();
            // $res_code = $ztouch->checkVerify($data['phone'], $data['code']); // 验证验证码
            $res_code = true;
            if ($res_code == true) {
                $user_info = $this->common->check_phone($data['phone'], Request()->ip(), $data['remote_device'], $data['lat'], $data['lng']);
                if ($user_info == false) {return json_return(304, '验证手机失败，请联系管理员');}
                $res = model('UserWx')->save(['user_id' => $user_info['user_info']['id']], ['open_id' => $data['open_id']]);
                return $res ? json_return(200, '绑定手机成功', $user_info) : json_return(303, '绑定微信失败，请联系管理员');
            } else {
                return json_return(302, '验证码错误');
            }
        } else {
            return json_return(399, '非法请求');
        }

    }

    public function testTime()
    {
        $timer = new Time();
        $today = $timer->today();
        $yesterday = $timer->yesterday();
        dump(date('Y-m-d H:i:s', $yesterday[0]));
        dump(date('Y-m-d H:i:s', $yesterday[1]));
    }

}
